/***2024-12-17 迷途小羔羊
* 用于编写网页dom操作的一些工具类
*/
namespace GYLite
{
    export class DomUtil
    {
        /**主动抛出一个鼠标事件
         * @param obj 抛出事件的dom对象
        */
        public static dispatch_click(obj:HTMLElement) {
            var ev = document.createEvent("MouseEvents");
            ev.initMouseEvent(
                "click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null
            );
            obj.dispatchEvent(ev);
        }
        /**下载文件通过url数组 ,调用方法 download([save.txt],[下载路径]);     
        */
        public static downloadUrls(names:string[], urls:string[], type:any = null) {        
            var save_link:any;
            let len:number;
            len = urls.length;
            while(--len>-1)
            {
                save_link = document.createElementNS("http://www.w3.org/1999/xhtml", "a")
                save_link.href = urls[len];
                save_link.download = (names && names[len])?names[len]:"f"+len;
                DomUtil.dispatch_click(save_link);
            }        
        }
        /**显示保存对话框                  
         * @param data 文件数据
         * @param filters mimeType 如{image/png}
         * @param title 对话框标题 默认 "保存文件"
         * @param defaultPath 默认对话框显示的路径 默认null
         * @param browserSave 保存完成回调，只有在客户端有效 callBack: (event: any, response: any) => void, response:{
         *          data: any;
         *          bytes: Uint8Array;
         *          response: Response;
         *          code: number;
         *          msg: string;
         *      }
        */
        public static browserSave(name: string, data: any, type: any = null, title: string = "保存文件", defaultPath: string = null, callBack: (event: any, response: any) => void = null): void {
            if(data==null)
            return;
            let api:any = window["API"];
            if (api == null) {
                DomUtil.browserDomSave(name, data, type)
                return null;
            }
            if(data.constructor == Blob)
            {
                (<Blob>data).arrayBuffer().then((value:ArrayBuffer)=>{
                    DomUtil.browserSave(name, new Uint8Array(value), type, title, defaultPath, callBack);
                });
                return;
            }                
            let filters: { name: string, extensions: string[] }[];
            let arr: string[];
            if (type) {
                arr = type.type.split("/");
                let ext: string;
                if (arr[1])//优先取mimeType
                    ext = arr[1];
                else {
                    let arr2: string[] = name.split(".");//其次取名字的扩展名，没有则默认""
                    if (arr2.length > 1) {
                        arr2.shift();
                        ext = arr2.join(".");
                    }
                    else
                        ext = "";
                }
                filters = [{ name: arr[0], extensions: [ext] }];
            }
            if(defaultPath == null)
                defaultPath = name;
            api.browserSave(data, filters, title, defaultPath);
            if(callBack)
                api.once("ELECTRON_BROWSER_SAVE", callBack);            
        }
        /**下载文件 ,调用方法 download("save.txt","内容");
         * @param name 下载的文件名
         * @param data ArrayBuffer 或者 Blob|Blob[] 或者 String
         * @param type mimeType {type:"image/png"}等
        */
        public static browserDomSave(name:string, data:any, type:any = null) {
            var urlObject:any = window.URL || window["webkitURL"] || window;
            var downloadData = GYLite.CommonUtil.GYIs(data,Blob)?data:new Blob(GYLite.CommonUtil.GYIs(data,Array)?data:[data],type);
            var save_link:any = document.createElementNS("http://www.w3.org/1999/xhtml", "a")
            save_link.href = urlObject.createObjectURL(downloadData);
            save_link.download = name;
            DomUtil.dispatch_click(save_link);
        }
        /**上传文件
         * @param callBack 选择文件之后的回调，返回参数blob，function(fileList:FileList):void{},multi为true的情况，参数为数组[blob,blob……]
         * @param thisObj 回调this指向
         * @param accept 接受的文件类型 如png图片 "image/png",任意类型图片"image/*"，多个文件类型逗号隔开
         * @param param 附件回调的参数
         * @param multi 是否上传多个文件
        */
        public static upload(callBack:Function,thisObj:any,accept:string="image/png",param:any=null,multi:boolean=false,webkitdirectory :boolean=false):void
        {                        
            var upload_link:any = document.createElementNS("http://www.w3.org/1999/xhtml", "input");
            upload_link.type = "file";
            upload_link.id = "file";
            upload_link.accept=accept;
            upload_link.param = param;
            upload_link.webkitdirectory=webkitdirectory;
            upload_link.multiple=multi;
            upload_link.onchange = function(e):void{
                if(callBack!=null)
                {                    
                    callBack.call(thisObj,e.target.files,e.target.param);
                }
                    
            };		
            upload_link.style = "filter:alpha(opacity=0);opacity:0;width: 0;height: 0;";		
            DomUtil.dispatch_click(upload_link);
        }
    }
}